package com.myk.game.lib.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;

import androidx.annotation.DrawableRes;

public class BitmapUtil {
    /**
     * 获取指定大小的图片
     */
    public static Bitmap getBitmap(Context context, @DrawableRes int sourceId, float width, float height) {
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), sourceId);
        if (bitmap.getWidth() == width && bitmap.getHeight() == height) {    //写这个判断，避createScaledBitmap()的坑
            return bitmap;
        }
        return Bitmap.createScaledBitmap(bitmap, (int) width, (int) height, true);
    }

    /**
     * 获取指定大小图片的Drawable
     */
    public static BitmapDrawable getBitmapDrawable(Context context, @DrawableRes int sourceId, float width, float height){
        return new BitmapDrawable(context.getResources(),getBitmap(context,sourceId,width,height));
    }
}
